using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using InfoSite.Helper;

namespace InfoSite.Data
{
    [DataObject]
    [Serializable]
    public partial class Slide
    {
        #region Constants

        private const string TableName = "[dbo].[Slide]";

        #endregion
	
	
        #region Fields

        #endregion
	
	
        #region Properties

        public int? SlideId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string Text { get; set; }
        public string PopupTitle { get; set; }
        public string PopupText { get; set; }
        public string Url { get; set; }
        public string Photo { get; set; }
        public string Thumbnail { get; set; }
        public int? Location { get; set; }

        #endregion
	
	
        #region Methods
	
	
        #region Mapping Methods
	
        protected void MapTo(DataSet ds)
        {
            if (ds == null)
                ds = new DataSet();

            if (ds.Tables[TableName] == null)
                ds.Tables.Add(TableName);
		
            ds.Tables[TableName].Columns.Add("SlideId", typeof(Int32) );
            ds.Tables[TableName].Columns.Add("Title", typeof(String) );
            ds.Tables[TableName].Columns.Add("Description", typeof(String) );
            ds.Tables[TableName].Columns.Add("Text", typeof(String) );
            ds.Tables[TableName].Columns.Add("PopupTitle", typeof(String));
            ds.Tables[TableName].Columns.Add("PopupText", typeof(String) );
            ds.Tables[TableName].Columns.Add("Url", typeof(String) );
            ds.Tables[TableName].Columns.Add("Photo", typeof(String) );
            ds.Tables[TableName].Columns.Add("Thumbnail", typeof(String) );
            ds.Tables[TableName].Columns.Add("Location", typeof(Int32));
		
            DataRow dr = ds.Tables[TableName].NewRow();

            dr["SlideId"] = SlideId ?? (object) DBNull.Value;
            dr["Title"] = Title ?? (object) DBNull.Value;
            dr["Description"] = Description ?? (object) DBNull.Value;
            dr["Text"] = Text ?? (object) DBNull.Value;
            dr["PopupTitle"] = PopupTitle ?? (object)DBNull.Value;
            dr["PopupText"] = PopupText ?? (object) DBNull.Value;
            dr["Url"] = Url ?? (object) DBNull.Value;
            dr["Photo"] = Photo ?? (object) DBNull.Value;
            dr["Thumbnail"] = Thumbnail ?? (object) DBNull.Value;
            dr["Location"] = Location ?? (object)DBNull.Value;

            ds.Tables[TableName].Rows.Add(dr);
        }
	
        protected void MapFrom(DataRow dr)
        {
            SlideId = dr["SlideId"] != DBNull.Value ? Convert.ToInt32(dr["SlideId"]) : SlideId = null;
            Title = dr["Title"] != DBNull.Value ? Convert.ToString(dr["Title"]) : Title = null;
            Description = dr["Description"] != DBNull.Value ? Convert.ToString(dr["Description"]) : Description = null;
            Text = dr["Text"] != DBNull.Value ? Convert.ToString(dr["Text"]) : Text = null;
            PopupTitle = dr["PopupTitle"] != DBNull.Value ? Convert.ToString(dr["PopupTitle"]) : PopupTitle = null;
            PopupText = dr["PopupText"] != DBNull.Value ? Convert.ToString(dr["PopupText"]) : PopupText = null;
            Url = dr["Url"] != DBNull.Value ? Convert.ToString(dr["Url"]) : Url = null;
            Photo = dr["Photo"] != DBNull.Value ? String.Format("{0}/{1}", ConfigReader.SlidePhotoPath, Convert.ToString(dr["Photo"])) : Photo = null;
            Thumbnail = dr["Thumbnail"] != DBNull.Value ? String.Format("{0}/{1}", ConfigReader.SlideThumbnailPath, Convert.ToString(dr["Thumbnail"])) : Photo = null;
            Location = dr["Location"] != DBNull.Value ? Convert.ToInt32(dr["Location"]) : Location = null;
        }

        private static List<Slide> MapFrom(DataSet ds)
        {
            // Initialise Collection.
            List<Slide> objects = new List<Slide>();
		
            // Validation.
            if (ds == null)
                throw new ApplicationException("Cannot map to dataset null.");
            if (ds.Tables[TableName].Rows.Count == 0)
                return objects;

            if (ds.Tables[TableName] == null)
                throw new ApplicationException("Cannot find table [dbo].[Slide] in DataSet.");
		
            if (ds.Tables[TableName].Rows.Count < 1)
                throw new ApplicationException("Table [dbo].[Slide] is empty.");
		
            // Map DataSet to Instance.
            foreach (DataRow dr in ds.Tables[TableName].Rows)
            {
                Slide instance = new Slide();
                instance.MapFrom(dr);
                objects.Add(instance);
            }
		
            // Return collection.
            return objects;
        }
	
	
        #endregion
	
	
        #region CRUD Methods
	
        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public static Slide Get(Int32? slideId)
        {
            Slide instance = new Slide();
		
            Database db = DatabaseFactory.CreateDatabase();
            const string sqlCommand = "[dbo].gspSlide_SELECT";
            using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, slideId))
            {
                using (DataSet ds = db.ExecuteDataSet(dbCommand))
                {
                    if (ds == null || ds.Tables[0].Rows.Count == 0) throw new ApplicationException(string.Format("Could not get Slide ID:{0} from Database.", slideId));
                    // Return results.
                    ds.Tables[0].TableName = TableName;
		
                    instance.MapFrom( ds.Tables[0].Rows[0] );
                }
            }

            return instance;
        }
	
        #region INSERT
        private void Insert(String title, String description, String text, String popupTitle, String popupText, String url, String photo, String thumbnail, Int32? location, DbTransaction transaction = null)
        {
            Database db = DatabaseFactory.CreateDatabase();
            const string sqlCommand = "[dbo].gspSlide_INSERT";
            using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, title, description, text, popupTitle, popupText, url, photo, thumbnail, location))
            {
                SlideId = Convert.ToInt32(transaction == null ? db.ExecuteScalar(dbCommand) : db.ExecuteScalar(dbCommand, transaction));
            }
        }

        /// <summary>
        /// Insert current Slide to database.
        /// </summary>
        /// <param name="transaction">optional SQL Transaction</param>
        private void Insert(DbTransaction transaction = null)
        {
            Insert(Title, Description, Text, PopupTitle, PopupText, Url, Photo, Thumbnail, Location, transaction);
        }

        #endregion
	
	
        #region UPDATE
        private static void Update(Int32? slideId, String title, String description, String text, String popupTitle, String popupText, String url, String photo, String thumbnail, Int32? location, DbTransaction transaction)
        {
            Database db = DatabaseFactory.CreateDatabase();
            const string sqlCommand = "[dbo].gspSlide_UPDATE";
            DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
            db.DiscoverParameters(dbCommand);
            dbCommand.Parameters["@slideId"].Value = slideId;
            dbCommand.Parameters["@title"].Value = title;
            dbCommand.Parameters["@description"].Value = description;
            dbCommand.Parameters["@text"].Value = text;
            dbCommand.Parameters["@popupTitle"].Value = popupTitle;
            dbCommand.Parameters["@popupText"].Value = popupText;
            dbCommand.Parameters["@url"].Value = url;
            dbCommand.Parameters["@photo"].Value = photo;
            dbCommand.Parameters["@thumbnail"].Value = thumbnail;
            dbCommand.Parameters["@location"].Value = location;
		
            if (transaction == null)
                db.ExecuteNonQuery(dbCommand);
            else
                db.ExecuteNonQuery(dbCommand, transaction);
        }
	
        [DataObjectMethod(DataObjectMethodType.Update, true)]
        private static void Update(Int32? slideId, String title, String description, String text, String popupTitle, String popupText, String url, String photo, String thumbnail, Int32 location)
        {
            Update(slideId, title, description, text, popupTitle, popupText, url, photo, thumbnail, location, null);
        }
	
        private static void Update(Slide slide)
        {
            slide.Update();
        }
	
        private static void Update(Slide slide, DbTransaction transaction)
        {
            slide.Update(transaction);
        }
	
        /// <summary>
        /// Updates changes to the database.
        /// </summary>
        /// <param name="transaction">optional SQL Transaction</param>
        public void Update(DbTransaction transaction = (DbTransaction)null)
        {
            Database db = DatabaseFactory.CreateDatabase();
            const string sqlCommand = "[dbo].gspSlide_UPDATE";
            using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
            {
                db.DiscoverParameters(dbCommand);
                dbCommand.Parameters["@slideId"].SourceColumn = "SlideId";
                dbCommand.Parameters["@title"].SourceColumn = "Title";
                dbCommand.Parameters["@description"].SourceColumn = "Description";
                dbCommand.Parameters["@text"].SourceColumn = "Text";
                dbCommand.Parameters["@popupTitle"].SourceColumn = "PopupTitle";
                dbCommand.Parameters["@popupText"].SourceColumn = "PopupText";
                dbCommand.Parameters["@url"].SourceColumn = "Url";
                dbCommand.Parameters["@photo"].SourceColumn = "Photo";
                dbCommand.Parameters["@thumbnail"].SourceColumn = "Thumbnail";
                dbCommand.Parameters["@location"].SourceColumn = "Location";

                using (DataSet ds = new DataSet())
                {
                    MapTo( ds );
                    ds.AcceptChanges();
                    ds.Tables[0].Rows[0].SetModified();
                    if (transaction == null)
                        db.UpdateDataSet(ds, TableName, null, dbCommand, null, UpdateBehavior.Standard);
                    else
                        db.UpdateDataSet(ds, TableName, null, dbCommand, null, transaction);
                }
            }
        }

        #endregion
	
	
        #region DELETE
        [DataObjectMethod(DataObjectMethodType.Delete, false)]
        private static void Delete(Int32? slideId, DbTransaction transaction = null)
        {
            Database db = DatabaseFactory.CreateDatabase();
            const string sqlCommand = "[dbo].gspSlide_DELETE";
            using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, slideId))
            {
                if (transaction != null)
                {
                    db.ExecuteNonQuery(dbCommand, transaction);
                }
                else
                {
                    db.ExecuteNonQuery(dbCommand);
                }
            }
        }

        /// <summary>
        /// Delete current Slide from database.
        /// </summary>
        /// <param name="transaction">optional SQL Transaction</param>
        private void Delete(DbTransaction transaction = (DbTransaction)null)
        {
            Database db = DatabaseFactory.CreateDatabase();
            const string sqlCommand = "[dbo].gspSlide_DELETE";
            using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, SlideId))
            {
                if (transaction != null)
                {
                    db.ExecuteNonQuery(dbCommand, transaction);
                }
                else
                {
                    db.ExecuteNonQuery(dbCommand);
                }
            }
            SlideId = null;
        }

        #endregion
	
	
        #region SEARCH
        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public static List<Slide> Search(Int32? slideId = null, String title = null, String description = null, String text = null, String popupTitle = null, String popupText = null, String url = null, String photo = null, String thumbnail = null, Int32? location = null)
        {
            Database db = DatabaseFactory.CreateDatabase();
            const string sqlCommand = "[dbo].gspSlide_SEARCH";
            using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, slideId, title, description, text, popupTitle, popupText, url, photo, thumbnail, location))
            {
                using (DataSet ds = db.ExecuteDataSet(dbCommand))
                {
                    ds.Tables[0].TableName = TableName;
                    return MapFrom(ds);
                }
            }
        }
	
	
        public static List<Slide> Search(Slide searchObject)
        {
            return Search ( searchObject.SlideId, searchObject.Title, searchObject.Description, searchObject.Text, searchObject.PopupTitle, searchObject.PopupText, searchObject.Url, searchObject.Photo, searchObject.Thumbnail, searchObject.Location);
        }

        #endregion
	
	
        #endregion
	
	
        #endregion
    }
}

